class UsersController < ApplicationController
  before_action :auth_user, only: [:index]

  def index
    # 定义列表
    @users = User.all.order(id: :desc)
  end

  def new
    @user = User.new
  end

  def create
    # params[:user] => {username: 'xxx', password: 'xx'}
    @user = User.create user_attrs
    if @user
      flash[:notice] = '注册成功'
      redirect_to new_session_path
    else
      render action: :new
    end
  end

  
  def blogs
    @blogs = current_user.blogs
  end

  private 

  def user_attrs
    # 当前 params 需要有一个 user key
    # 只接收 username password 字段
    params.require(:user).permit(:username, :password, :password_confirmation)
  end
end
